class Cart
  attr_accessor :order_id

  def add_items hash
    hash.each do |key, value|
      if !value.nil? && !key.nil? && value.to_i > 0
        if @items.key? key.to_i
          @items[key.to_i] += value.to_i
        else
          @items[key.to_i] = value.to_i
        end
      end
    end
  end

  def delete_item id
    @items.delete id.to_i if(id && @items.key?(id.to_i))
  end

  def update_items hash
    hash.each do |key, value|
      @items[key.to_i] = value.to_i if !value.nil? && !key.nil? && @items.key?(key.to_i) && value.to_i > 0
    end
  end

  def initialize
    @items = Hash.new
    @order_id = nil
  end

  def get_items
    @items
  end

  def get_total
    quantity = 0
    price = 0
    @items.each do |key, value|
      quantity += value
      price += Product.find(key).price * value
    end
    {:quantity => quantity, :price => price}
  end

  def clear
    @items = Hash.new
    @order_id = nil
  end

  def empty?
    !(@items && @items.size > 0)
  end
end
